package controller.${table.module};

import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import core.modules.${table.module}.entity.${table.javaName?cap_first};
import core.modules.${table.module}.service.${table.javaName?cap_first}Service;
import core.util.AjaxResult;
import core.util.Page;
import core.util.WebUtil;
/**
 * ${table.remarks}Controller
 * @author ${table.author}
 */
@Controller
@RequestMapping("${table.module?uncap_first}/${table.javaName?uncap_first}")
public class ${table.javaName?cap_first}Controller {
	Logger LOGGER = LoggerFactory.getLogger(${table.javaName?cap_first}Controller.class);

	@Autowired
	${table.javaName?cap_first}Service ${table.javaName?uncap_first}Service;

	@RequestMapping("list.htm")
	public Object table(${table.javaName?cap_first} condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		Page<${table.javaName?cap_first}> page = ${table.javaName?uncap_first}Service.findPage(condition);
		map.put("param", condition);
		return "${table.module?uncap_first}/${table.javaName?uncap_first}/${table.javaName?uncap_first}List";
	}
	
	@RequestMapping("form.htm")
	public String form(${table.javaName?cap_first} condition, HttpServletRequest request, HttpServletResponse response, ModelMap map) {
		if (WebUtil.isNotBlank(condition.getId())) {
			condition= ${table.javaName?uncap_first}Service.get(condition.getId());
		}
		map.put("param", condition);
		map.put("viewMode", WebUtil.getStr("viewMode", request));
		return "${table.module?uncap_first}/${table.javaName?uncap_first}/${table.javaName?uncap_first}Form";
	}

	@RequestMapping("save.json")
	@ResponseBody
	public AjaxResult save(${table.javaName?cap_first} condition, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			${table.javaName?uncap_first}Service.save(condition);
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "保存失败:" + e.getMessage());
		}
		return result;
	}

	@RequestMapping("delete.json")
	@ResponseBody
	public AjaxResult delete(String ids, HttpServletRequest request, HttpServletResponse response) {
		AjaxResult result = null;
		try {
			String[] idArray=ids.split(",");
			for (String id : idArray) {
				if (WebUtil.isNotBlank(id)) {
					${table.javaName?uncap_first}Service.delete(id);
				}
			}
			result = new AjaxResult();
		} catch (Exception e) {
			result = new AjaxResult(false, "删除失败:" + e.getMessage());
		}
		return result;
	}
}
